package gui;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import parsing.productions.Rule;

import world.Critter;

public class CurrentRulePanel extends JPanel {

	private JLabel ruleText;
	
	public CurrentRulePanel(){
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		JPanel title =  new JPanel();
		title.add(new JLabel("Last rule executed:"));
		JPanel rule = new JPanel();
		ruleText = new JLabel();
		rule.add(ruleText);
		add(title);
		add(rule);
	}
	
	/**
	 * Updates the rule displayed by this panel to the last rule executed by
	 * the given Critter or resets the display if c is null
	 * @param c The Critter whose last rule is to be displayed
	 */
	public void updateRule(Critter c){
		if (c != null){
			Rule rule = c.getLastRule();
			if(rule != null){
				StringBuffer sb = new StringBuffer();
				rule.prettyPrint(sb);
				ruleText.setText(sb.toString());
			} else
				ruleText.setText("");
		} else
			ruleText.setText("");
	}
}
